﻿using Microsoft.Xrm.Sdk;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using Microsoft.Xrm.Sdk.Query;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.7.0.1
 * 13 Dec 2011
 **/
namespace Gemina.CRM2011.WF.LdgManager
{
/*    [AgCodeActivity(ActivityNameCloneUserViews, AgViewsUtils.GroupName)]
    public class AgCloneUserViews : AgCodeActivityBase
    {

        #region Constants

        private const string ActivityNameCloneUserViews = "Clone User Views";
        private const string Param1InputViewDefinitions = "View Definitions";
        private const string Param2TargetUser = "Target User";

        #endregion

        #region Properties

        #endregion

        #region Workflow Input Parameters

        /// <summary>
        /// Input parameter
        /// </summary>
        [Input(Param1InputViewDefinitions)]
        [RequiredArgument]
        public InArgument<String> InputViewDefinitions { get; set; }

        /// <summary>
        /// Input parameter
        /// </summary>
        [Input(Param2TargetUser)]
        [ReferenceTarget(AgViewsUtils.EntityNameSystemUser)]
        [RequiredArgument]
        public InArgument<EntityReference> TargetUser { get; set; }

                /// <summary>
                /// Input parameter
                /// </summary>
                [Input(Param1InputUserQueryNameRegex)]
                public InArgument<String> InputUserQueryNameRegex { get; set; }

                /// <summary>
                /// Input parameter
                /// </summary>
                [Input(Param2InputUserQueryOwner)]
                [ReferenceTarget(AgViewsUtils.EntityNameSystemUser)]
                [RequiredArgument]
                public InArgument<EntityReference> InputUserQueryOwner { get; set; }

                /// <summary>
                /// Input parameter
                /// </summary>
                [Input(Param3InputUserViewReturnedTypeRegex)]
                public InArgument<String> InputEntityLogicalNameRegex { get; set; }

                /// <summary>
                /// Input parameter
                /// </summary>
                [Input(Param4InputUserQueryTypeRegex)]
                public InArgument<String> InputQueryTypeRegex { get; set; }
        #endregion

        #region Workflow Output Parameters
        /// <summary>
        /// Output parameter
        /// </summary>
        [Output(Param1OutputViewCount)]
        public OutArgument<int> OutputViewCount { get; set; }

        /// <summary>
        /// Output parameter
        /// </summary>
        [Output(Param2OutputViewDefinitions)]
        public OutArgument<String> OutputViewDefinitions { get; set; }

 #endregion

        #region CodeActivity

        override protected bool ExecuteBody(CodeActivityContext executionContext)
        {
            return true;
        }

        #endregion

        #region Local Methods
        /*
        private void CheckArguments(CodeActivityContext executionContext)
        {
            string queryTypePattern = this.InputQueryTypeRegex.Get(executionContext);
            this.ValidatedUserQueryOwner = this.InputUserQueryOwner.Get(executionContext);
            string returnedTypeRegex = this.InputEntityLogicalNameRegex.Get(executionContext);
            string nameRegex = this.InputUserQueryNameRegex.Get(executionContext);

            if (this.ValidatedUserQueryOwner == null)
            {
                throw new AgErrorMessage(string.Format(MsgNull, Param2InputUserQueryOwner));
            }

            Regex queryTypeRegex = null;
            if (string.IsNullOrEmpty(queryTypePattern))
            {
                queryTypeRegex = new Regex(".*", RegexOptions.IgnoreCase);
            }
            else
            {
                try
                {
                    queryTypeRegex = new Regex(queryTypePattern, RegexOptions.IgnoreCase);
                }
                catch (ArgumentException ex)
                {
                    throw new AgErrorMessage(string.Format(MsgInvalidRegex, Param4InputUserQueryTypeRegex, queryTypePattern, ex.Message));
                }
            }
            this.ValidatedQueryTypes = AgViewsUtils.GetUserQueryTypes(queryTypeRegex);

            if (string.IsNullOrEmpty(returnedTypeRegex))
            {
                this.ValidatedEntityLogicalNameRegex = new Regex(".*", RegexOptions.IgnoreCase);
            }
            else
            {
                try
                {
                    this.ValidatedEntityLogicalNameRegex = new Regex(returnedTypeRegex, RegexOptions.IgnoreCase);
                }
                catch (ArgumentException ex)
                {
                    throw new AgErrorMessage(string.Format(MsgInvalidRegex, Param3InputUserViewReturnedTypeRegex, returnedTypeRegex, ex.Message));
                }
            }

            if (string.IsNullOrEmpty(nameRegex))
            {
                this.ValidatedUserQueryNameRegex = new Regex(".*", RegexOptions.IgnoreCase);
            }
            else
            {
                try
                {
                    this.ValidatedUserQueryNameRegex = new Regex(nameRegex, RegexOptions.IgnoreCase);
                }
                catch (ArgumentException ex)
                {
                    throw new AgErrorMessage(string.Format(MsgInvalidRegex, Param1InputUserQueryNameRegex, nameRegex, ex.Message));
                }
            }
        }

        private void RetrieveUserQueries(CodeActivityContext executionContext)
        {
            QueryExpression query = new QueryExpression
            {
                EntityName = AgViewsUtils.EntityNameUserQuery,
                ColumnSet = new ColumnSet(true),
                Criteria = new FilterExpression()
            };

            Guid user = this.ValidatedUserQueryOwner.Id;
            IOrganizationService svc = this.GetService(executionContext, user);

            DataCollection<Entity> responses = svc.RetrieveMultiple(query).Entities;

            this.SetOutputValues(responses, executionContext);
        }

        private void SetOutputValues(DataCollection<Entity> views, CodeActivityContext executionContext)
        {
            List<Entity> filteredViews = new List<Entity>();
            foreach (Entity entity in views)
            {
                if (this.ValidatedUserQueryNameRegex.IsMatch((string)entity.Attributes[AgViewsUtils.FieldName]))
                {
                    if (this.ValidatedEntityLogicalNameRegex.IsMatch((string)entity.Attributes[AgViewsUtils.FieldReturnedTypeCode]))
                    {
                        if (this.ValidatedQueryTypes.Contains((int)entity.Attributes[AgViewsUtils.FieldQueryType]))
                        {
                            filteredViews.Add(entity);
                        }
                    }
                }
            }
            this.OutputViewCount.Set(executionContext, filteredViews.Count);
            this.OutputViewDefinitions.Set(executionContext, this.Serialize(filteredViews));
        }

 #endregion

    }
    */
}
